//
//  ViewController.m
//  Study1
//
//  Created by ChenJie on 2/3/20.
//  Copyright © 2020 OrgName. All rights reserved.
//

#import "ViewController.h"

typedef NS_ENUM(NSUInteger, ButtonTags) {
    ButtonTag_button1 = 1,
    ButtonTag_button2,
};

#define kLabelText @"三是各级妇幼保"
#define kLabelFont [UIFont systemFontOfSize:14]

#define kTextViewText @"三是各级妇幼保健机构要按照当地卫生健康行政部门的统一部署开展疫情防控工作，按照新型冠状病毒感染的肺炎诊疗方案做好临床诊疗工作。在疫情期间合理调整儿童保健门诊和预防接种门诊，暂停亲子活动、家长学校等我"
#define kTextViewFont [UIFont systemFontOfSize:24]

#define kItems @[@"11", @"22", @"33"]

@interface ViewController () <
    UITextFieldDelegate,
    UITextViewDelegate
>

//strong - 对象
//weak - 基本用不上，代理
//assign - 基础数据

@property (nonatomic, strong) UILabel *label1111;
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) UIButton *button2;
@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UITextView *textView;
@property (nonatomic, strong) UISegmentedControl *segmentedControl;
@property (nonatomic, strong) UISlider *slider;

@end

@implementation ViewController

#pragma mark - ViewController lifecycle

- (instancetype)init {
    self = [super init];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = NSStringFromClass(self.class);
    
    //
    [self.view addSubview:self.label1111];
    self.label1111.text = kLabelText;
    
    //
    [self.view addSubview:self.button];
    [self.view addSubview:self.button2];
    [self.view addSubview:self.textField];
    [self.view addSubview:self.textView];
    self.textView.text = kTextViewText;
    
    [self.view addSubview:self.segmentedControl];
    self.segmentedControl.selectedSegmentIndex = 0;
    
    [self.view addSubview:self.slider];
    self.slider.value = 0.2;
    
//    self.button.selected = YES;
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    CGSize viewSize = self.view.frame.size;
    
    CGFloat topMark = 0;
    CGFloat spaceBetweenV = 10;
    
    CGFloat left = 10;
    CGFloat right = 10;
    CGFloat contentWidth = viewSize.width - left - right;
    CGFloat labelHeight = 44;
    CGFloat buttonHeight = 44;
    CGFloat textFieldHeight = 44;
    CGFloat textViewHeight = 50;
    CGFloat segmentedHeight = 35;
    CGFloat sliderHeight = 50;
    
    //
    topMark = [UIApplication sharedApplication].statusBarFrame.size.height;
    topMark += self.navigationController.navigationBar.frame.size.height;
    topMark += 10;
    
    CGSize labelSize = [kLabelText boundingRectWithSize:CGSizeMake(contentWidth, CGFLOAT_MAX)
                                                options:NSStringDrawingUsesLineFragmentOrigin
                                             attributes:@{NSFontAttributeName: kLabelFont}
                                                context:nil].size;
    labelHeight = labelSize.height;
    
    self.label1111.frame = CGRectMake(left, topMark, contentWidth, labelHeight);
    topMark += labelHeight;
    
    //
    topMark += spaceBetweenV;
    self.button.frame = CGRectMake(left, topMark, contentWidth, buttonHeight);
    topMark += buttonHeight;
    
    //
    topMark += spaceBetweenV;
    self.button2.frame = CGRectMake(left, topMark, contentWidth, buttonHeight);
    topMark += buttonHeight;
    
    //
    topMark += spaceBetweenV;
    self.textField.frame = CGRectMake(left, topMark, contentWidth, textFieldHeight);
    topMark += textFieldHeight;
    
    //
    topMark += spaceBetweenV;
    self.textView.frame = CGRectMake(left, topMark, contentWidth, textViewHeight);
    topMark += textViewHeight;
    
    //
    topMark += spaceBetweenV;
    self.segmentedControl.frame = CGRectMake(left, topMark, contentWidth, segmentedHeight);
    topMark += segmentedHeight;
    
    //
    topMark += spaceBetweenV;
    self.slider.frame = CGRectMake(left, topMark, contentWidth, sliderHeight);
    topMark += sliderHeight;
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDefault;
}

#pragma mark - Public methods



#pragma mark - Private methods



#pragma mark - Event response methods

- (void)buttonDidiClick:(UIButton *)button {
    NSLog(@"button clicked...");
    
    NSString *text = button.tag == ButtonTag_button1 ? @"button 1 clicked." : @"button 2 clicked..";
    
    self.label1111.text = text;
    
    [self.textField resignFirstResponder];
    [self.textView resignFirstResponder];
}

- (void)textFieldDidChange:(UITextField *)textField {
    NSLog(@"sdfdsf: %@", textField.text);
}

- (void)segmentedControlDidSelect:(UISegmentedControl *)segementControl {
    NSLog(@"current value: %@", kItems[segementControl.selectedSegmentIndex]);
}

- (void)sliderDidChange:(UISlider *)slider {
    self.textField.text = [NSString stringWithFormat:@"%f", slider.value];
}

#pragma mark - Deletate methods



#pragma mark - <UITextFieldDelegate>

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    NSLog(@"now return...");
    
    [textField resignFirstResponder];
    
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    NSLog(@"now did change: %@", textField.text);
}

#pragma mark - <UITextViewDelegate>

- (void)textViewDidChange:(UITextView *)textView {
    NSLog(@"now change: %@", textView.text);
}

// onfucus
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
    
//    CGRect viewFrame = self.view.frame;
//    viewFrame.origin.y -= 100;
//    self.view.frame = viewFrame;
    
    return YES;
}

// onblur
- (BOOL)textViewShouldEndEditing:(UITextView *)textView {
    
//    CGRect viewFrame = self.view.frame;
//    viewFrame.origin.y += 100;
//    self.view.frame = viewFrame;
    
    return YES;
}


#pragma mark - Setter



#pragma mark - Getter

- (UILabel *)label1111 {
    if (!_label1111) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
        label.backgroundColor = [UIColor lightGrayColor];
        label.numberOfLines = 0;
        label.textAlignment = NSTextAlignmentLeft;
        label.font = kLabelFont;
        
        _label1111 = label;
    }
    return _label1111;
}

- (UIButton *)button {
    if (!_button) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        
        //
        button.backgroundColor = [UIColor grayColor];
        button.tag = ButtonTag_button1;
        
        //
        [button setTitle:@"Button" forState:UIControlStateNormal];
        [button setTitle:@"Button highLight" forState:UIControlStateHighlighted];
        [button setTitle:@"Button Selected" forState:UIControlStateSelected];
        
        //
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor yellowColor] forState:UIControlStateHighlighted];
        [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateSelected];
        
        // target-action
        [button addTarget:self
                   action:@selector(buttonDidiClick:)
         forControlEvents:UIControlEventTouchUpInside];
        
        _button  = button;
    }
    return _button;
}

- (UIButton *)button2 {
    if (!_button2) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        
        //
        button.backgroundColor = [UIColor grayColor];
        button.tag = ButtonTag_button2;
        
        //
        [button setTitle:@"Button2" forState:UIControlStateNormal];
        [button setTitle:@"Button2 highLight" forState:UIControlStateHighlighted];
        [button setTitle:@"Button2 Selected" forState:UIControlStateSelected];
        
        //
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor yellowColor] forState:UIControlStateHighlighted];
        [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateSelected];
        
        // target-action
        [button addTarget:self
                   action:@selector(buttonDidiClick:)
         forControlEvents:UIControlEventTouchUpInside];
        
        _button2  = button;
    }
    return _button2;
}

- (UITextField *)textField {
    if (!_textField) {
        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectZero];
        textField.backgroundColor = [UIColor lightGrayColor];
        textField.delegate = self;
        
        textField.placeholder = @"this is a textfield";
        
        textField.keyboardType = UIKeyboardTypeNumberPad;
        textField.keyboardType = UIKeyboardTypeDecimalPad;
        textField.keyboardType = UIKeyboardTypeURL;
        
//        textField.secureTextEntry = YES;
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        textField.autocorrectionType = UITextAutocorrectionTypeNo;
        textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
        
        [textField addTarget:self
                      action:@selector(textFieldDidChange:)
            forControlEvents:UIControlEventEditingChanged];
        
        _textField = textField;
    }
    return _textField;
}

- (UITextView *)textView {
    if (!_textView) {
        UITextView *textView = [[UITextView alloc] initWithFrame:CGRectZero];
        textView.backgroundColor = [UIColor lightGrayColor];
        textView.font = kTextViewFont;
        textView.delegate = self;
        textView.keyboardType = UIKeyboardTypeURL;
        textView.autocorrectionType = UITextAutocorrectionTypeNo;
        
        _textView = textView;
    }
    return _textView;
}

- (UISegmentedControl *)segmentedControl {
    if (!_segmentedControl) {
        UISegmentedControl *segmentControl = [[UISegmentedControl alloc] initWithItems:kItems];
        [segmentControl addTarget:self
                           action:@selector(segmentedControlDidSelect:)
                 forControlEvents:UIControlEventValueChanged];
        _segmentedControl = segmentControl;
    }
    return _segmentedControl;
}

- (UISlider *)slider {
    if (!_slider) {
        UISlider *slider = [UISlider new];
        [slider addTarget:self
                   action:@selector(sliderDidChange:)
         forControlEvents:UIControlEventValueChanged];
        
        _slider = slider;
    }
    
    return _slider;
}

#pragma mark - Memory management

- (void)dealloc {
}

@end
